package lg.minecraft.plugin.library.command.arg;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

import java.util.function.Predicate;

/**
 * @author langle__
 * @version 1.0
 */
public final class ArgType {

    public static final ArgType STRING = new ArgType(arg -> true, "");
    public static final ArgType PLAYER = new ArgType(arg -> Bukkit.getPlayer(arg) != null, "玩家不存在");
    public static final ArgType NUMBER = new ArgType(arg -> arg.matches("-?\\d+(\\.\\d+)?"), "无效数值");
    public static final ArgType BOOLEAN = new ArgType(arg -> arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("false"), "无效参数类型");

    @NotNull public final String errorMessage;
    @NotNull public final Predicate<String> predicate;

    public ArgType(@NotNull Predicate<String> predicate, @NotNull String errorMessage) {
        this.predicate = predicate;
        this.errorMessage = errorMessage;
    }

    public boolean validate(String arg) {
        return predicate.test(arg);
    }

}

